transition_spec.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. require 'api_client'
  2. RSpec.describe 'Transitions' do
  3. before(:all) do
  4. @client = ApiClient.from_environment
  5. @client.upload_json('/settings', 'settings.json')
  6. @transition_params = {
  7. field: 'level',
  8. start_value: 0,
  9. end_value: 100,
  10. duration: 2.0,
  11. period: 400
  12. }
  13. @num_transition_updates = (@transition_params[:duration]*1000)/@transition_params[:period]
  14. end
  15. before(:each) do
  16. mqtt_params = mqtt_parameters()
  17. @updates_topic = mqtt_params[:updates_topic]
  18. @topic_prefix = mqtt_topic_prefix()
  19. @client.put(
  20. '/settings',
  21. mqtt_params.merge(
  22. mqtt_update_topic_pattern: "#{@topic_prefix}updates/:device_id/:device_type/:group_id"
  23. )
  24. )
  25. @id_params = {
  26. id: @client.generate_id,
  27. type: 'rgb_cct',
  28. group_id: 1
  29. }
  30. @client.delete_state(@id_params)
  31. @mqtt_client = create_mqtt_client()
  32. # Delete any existing transitions
  33. @client.get('/transitions')['transitions'].each do |t|
  34. @client.delete("/transitions/#{t['id']}")
  35. end
  36. end
  37. context 'REST routes' do
  38. it 'should respond with an empty list when there are no transitions' do
  39. response = @client.transitions
  40. expect(response).to eq([])
  41. end
  42. it 'should respond with an error when missing parameters for POST /transitions' do
  43. expect { @client.post('/transitions', {}) }.to raise_error(Net::HTTPServerException)
  44. end
  45. it 'should create a new transition with a valid POST /transitions request' do
  46. response = @client.schedule_transition(@id_params, @transition_params)
  47. expect(response['success']).to eq(true)
  48. end
  49. it 'should list active transitions' do
  50. @client.schedule_transition(@id_params, @transition_params)
  51. response = @client.transitions
  52. expect(response.length).to be >= 1
  53. end
  54. it 'should support getting an active transition with GET /transitions/:id' do
  55. @client.schedule_transition(@id_params, @transition_params)
  56. response = @client.transitions
  57. detail_response = @client.get("/transitions/#{response.last['id']}")
  58. expect(detail_response['period']).to_not eq(nil)
  59. end
  60. it 'should support deleting active transitions with DELETE /transitions/:id' do
  61. @client.schedule_transition(@id_params, @transition_params)
  62. response = @client.transitions
  63. response.each do |transition|
  64. @client.delete("/transitions/#{transition['id']}")
  65. end
  66. after_delete_response = @client.transitions
  67. expect(response.length).to eq(1)
  68. expect(after_delete_response.length).to eq(0)
  69. end
  70. end
  71. context '"transition" key in state update' do
  72. it 'should create a new transition' do
  73. @client.patch_state({status: 'ON', level: 0}, @id_params)
  74. @client.patch_state({level: 100, transition: 2.0}, @id_params)
  75. response = @client.transitions
  76. expect(response.length).to be > 0
  77. expect(response.last['type']).to eq('field')
  78. expect(response.last['field']).to eq('level')
  79. expect(response.last['end_value']).to eq(100)
  80. @client.delete("/transitions/#{response.last['id']}")
  81. end
  82. it 'should transition field' do
  83. seen_updates = 0
  84. last_value = nil
  85. @client.patch_state({status: 'ON', level: 0}, @id_params)
  86. @mqtt_client.on_update(@id_params) do |id, msg|
  87. if msg.include?('brightness')
  88. seen_updates += 1
  89. last_value = msg['brightness']
  90. end
  91. last_value == 255
  92. end
  93. @client.patch_state({level: 100, transition: 2.0}, @id_params)
  94. @mqtt_client.wait_for_listeners
  95. expected_updates = calculate_transition_steps(start_value: 0, end_value: 255, duration: 2000)
  96. expect(last_value).to eq(255)
  97. expect(seen_updates).to eq(expected_updates.length)
  98. end
  99. it 'should transition a field downwards' do
  100. seen_updates = 0
  101. last_value = nil
  102. @client.patch_state({status: 'ON'}, @id_params)
  103. @client.patch_state({level: 100}, @id_params)
  104. @mqtt_client.on_update(@id_params) do |id, msg|
  105. if msg.include?('brightness')
  106. seen_updates += 1
  107. last_value = msg['brightness']
  108. end
  109. last_value == 0
  110. end
  111. @client.patch_state({level: 0, transition: 2.0}, @id_params)
  112. @mqtt_client.wait_for_listeners
  113. expected_updates = calculate_transition_steps(start_value: 0, end_value: 255, duration: 2000)
  114. expect(last_value).to eq(0)
  115. expect(seen_updates).to eq(expected_updates.length) # duration of 2000ms / 450ms period + 1 for initial packet
  116. end
  117. it 'should transition two fields at once if received in the same command' do
  118. updates = {}
  119. @client.patch_state({status: 'ON', hue: 0, level: 100}, @id_params)
  120. @mqtt_client.on_update(@id_params) do |id, msg|
  121. msg.each do |k, v|
  122. updates[k] ||= []
  123. updates[k] << v
  124. end
  125. updates['hue'] && updates['brightness'] && updates['hue'].last == 250 && updates['brightness'].last == 0
  126. end
  127. @client.patch_state({level: 0, hue: 250, transition: 2.0}, @id_params)
  128. @mqtt_client.wait_for_listeners
  129. expected_updates = calculate_transition_steps(start_value: 0, end_value: 250, duration: 2000)
  130. expect(updates['hue'].last).to eq(250)
  131. expect(updates['brightness'].last).to eq(0)
  132. expect(updates['hue'].length == updates['brightness'].length).to eq(true), "Should have the same number of updates for both fields"
  133. expect(updates['hue'].length).to eq(expected_updates.length)
  134. end
  135. end
  136. context 'transition packets' do
  137. it 'should send an initial state packet' do
  138. seen = false
  139. @mqtt_client.on_update(@id_params) do |id, message|
  140. seen = message['brightness'] == 0
  141. end
  142. @client.schedule_transition(@id_params, @transition_params)
  143. @mqtt_client.wait_for_listeners
  144. expect(seen).to be(true)
  145. end
  146. it 'should respect the period parameter' do
  147. seen_updates = []
  148. start_time = Time.now
  149. @mqtt_client.on_update(@id_params) do |id, message|
  150. seen_updates << message
  151. message['brightness'] == 255
  152. end
  153. @client.schedule_transition(@id_params, @transition_params.merge(duration: 2.0, period: 500))
  154. @mqtt_client.wait_for_listeners
  155. expected_updates = calculate_transition_steps(start_value: 0, end_value: 255, period: 500, duration: 2000)
  156. transitions_are_equal(
  157. expected: expected_updates,
  158. seen: seen_updates.map { |x| x['brightness'] },
  159. # Allow some variation for the lossy level -> brightness conversion
  160. allowed_variation: 2
  161. )
  162. expect((Time.now - start_time)/4).to be >= 0.5 # Don't count the first update
  163. end
  164. it 'should support two transitions for different devices at the same time' do
  165. id1 = @id_params
  166. id2 = @id_params.merge(type: 'fut089')
  167. @client.schedule_transition(id1, @transition_params)
  168. @client.schedule_transition(id2, @transition_params)
  169. id1_updates = []
  170. id2_updates = []
  171. @mqtt_client.on_update do |id, msg|
  172. if id[:type] == id1[:type]
  173. id1_updates << msg
  174. else
  175. id2_updates << msg
  176. end
  177. id1_updates.length == @num_transition_updates && id2_updates.length == @num_transition_updates
  178. end
  179. @mqtt_client.wait_for_listeners
  180. expect(id1_updates.length).to eq(@num_transition_updates)
  181. expect(id2_updates.length).to eq(@num_transition_updates)
  182. end
  183. it 'should assume initial state if one is not provided' do
  184. @client.patch_state({status: 'ON', level: 0}, @id_params)
  185. seen_updates = []
  186. @mqtt_client.on_update(@id_params) do |id, message|
  187. seen_updates << message
  188. message['brightness'] == 255
  189. end
  190. @client.schedule_transition(@id_params, @transition_params.reject { |x| x == :start_value }.merge(duration: 2, period: 500))
  191. @mqtt_client.wait_for_listeners
  192. transitions_are_equal(
  193. expected: calculate_transition_steps(start_value: 0, end_value: 255, duration: 2000, period: 500),
  194. seen: seen_updates.map { |x| x['brightness'] },
  195. # Allow some variation for the lossy level -> brightness conversion
  196. allowed_variation: 2
  197. )
  198. end
  199. end
  200. context 'status transition' do
  201. it 'should transition from off -> on' do
  202. seen_updates = {}
  203. @client.patch_state({status: 'OFF'}, @id_params)
  204. @mqtt_client.on_update(@id_params) do |id, message|
  205. message.each do |k, v|
  206. seen_updates[k] ||= []
  207. seen_updates[k] << v
  208. end
  209. seen_updates['brightness'] && seen_updates['brightness'].last == 255
  210. end
  211. @client.patch_state({status: 'ON', transition: 1.0}, @id_params)
  212. @mqtt_client.wait_for_listeners
  213. expect(seen_updates['state']).to eq(['ON'])
  214. transitions_are_equal(
  215. expected: calculate_transition_steps(start_value: 0, end_value: 255, duration: 1000),
  216. seen: seen_updates['brightness'],
  217. # Allow some variation for the lossy level -> brightness conversion
  218. allowed_variation: 3
  219. )
  220. end
  221. it 'should transition from on -> off' do
  222. seen_updates = {}
  223. @client.patch_state({status: 'ON', level: 100}, @id_params)
  224. @mqtt_client.on_update(@id_params) do |id, message|
  225. message.each do |k, v|
  226. seen_updates[k] ||= []
  227. seen_updates[k] << v
  228. end
  229. seen_updates['state'] == ['OFF']
  230. end
  231. @client.patch_state({status: 'OFF', transition: 1.0}, @id_params)
  232. @mqtt_client.wait_for_listeners
  233. expect(seen_updates['state']).to eq(['OFF'])
  234. transitions_are_equal(
  235. expected: calculate_transition_steps(start_value: 255, end_value: 0, duration: 1000),
  236. seen: seen_updates['brightness'],
  237. # Allow some variation for the lossy level -> brightness conversion
  238. allowed_variation: 3
  239. )
  240. end
  241. it 'should transition from off -> on from 0 to a provided brightness, event when there is a last known brightness' do
  242. seen_updates = {}
  243. @client.patch_state({status: 'ON', brightness: 99}, @id_params)
  244. @client.patch_state({status: 'OFF'}, @id_params)
  245. @mqtt_client.on_update(@id_params) do |id, message|
  246. message.each do |k, v|
  247. seen_updates[k] ||= []
  248. seen_updates[k] << v
  249. end
  250. seen_updates['brightness'] && seen_updates['brightness'].last == 128
  251. end
  252. @client.patch_state({status: 'ON', brightness: 128, transition: 1.0}, @id_params)
  253. @mqtt_client.wait_for_listeners
  254. transitions_are_equal(
  255. expected: calculate_transition_steps(start_value: 0, end_value: 128, duration: 1000),
  256. seen: seen_updates['brightness'],
  257. # Allow some variation for the lossy level -> brightness conversion
  258. allowed_variation: 4
  259. )
  260. end
  261. it 'should transition from off -> on from 0 to 100, even when there is a last known brightness' do
  262. seen_updates = {}
  263. @client.patch_state({status: 'ON', brightness: 99}, @id_params)
  264. @client.patch_state({status: 'OFF'}, @id_params)
  265. @mqtt_client.on_update(@id_params) do |id, message|
  266. message.each do |k, v|
  267. seen_updates[k] ||= []
  268. seen_updates[k] << v
  269. end
  270. seen_updates['brightness'] && seen_updates['brightness'].last == 255
  271. end
  272. @client.patch_state({status: 'ON', transition: 1.0}, @id_params)
  273. @mqtt_client.wait_for_listeners
  274. transitions_are_equal(
  275. expected: calculate_transition_steps(start_value: 0, end_value: 255, duration: 1000),
  276. seen: seen_updates['brightness'],
  277. # Allow some variation for the lossy level -> brightness conversion
  278. allowed_variation: 4
  279. )
  280. end
  281. it 'should transition from on -> off with known last brightness' do
  282. seen_updates = {}
  283. @client.patch_state({status: 'ON', brightness: 99}, @id_params)
  284. @mqtt_client.on_update(@id_params) do |id, message|
  285. message.each do |k, v|
  286. seen_updates[k] ||= []
  287. seen_updates[k] << v
  288. end
  289. seen_updates['state'] == ['OFF']
  290. end
  291. @client.patch_state({status: 'OFF', transition: 1.0}, @id_params)
  292. @mqtt_client.wait_for_listeners
  293. transitions_are_equal(
  294. expected: calculate_transition_steps(start_value: 99, end_value: 0, duration: 1000),
  295. seen: seen_updates['brightness'],
  296. # Allow some variation for the lossy level -> brightness conversion
  297. allowed_variation: 3
  298. )
  299. end
  300. it 'should not transition to 100% if a brightness is specified' do
  301. seen_updates = {}
  302. # Set a last known level
  303. @client.patch_state({status: 'ON', level: 0}, @id_params)
  304. @client.patch_state({status: 'OFF'}, @id_params)
  305. @mqtt_client.on_update(@id_params) do |id, message|
  306. message.each do |k, v|
  307. seen_updates[k] ||= []
  308. seen_updates[k] << v
  309. end
  310. seen_updates['brightness'] && seen_updates['brightness'].last == 128
  311. end
  312. @client.patch_state({status: 'ON', brightness: 128, transition: 1.0}, @id_params)
  313. @mqtt_client.wait_for_listeners
  314. expect(seen_updates['state']).to eq(['ON'])
  315. transitions_are_equal(
  316. expected: calculate_transition_steps(start_value: 0, end_value: 128, duration: 1000),
  317. seen: seen_updates['brightness'],
  318. # Allow some variation for the lossy level -> brightness conversion
  319. allowed_variation: 3
  320. )
  321. end
  322. end
  323. context 'field support' do
  324. {
  325. 'level' => {range: [0, 100], update_field: 'brightness', update_max: 255},
  326. 'brightness' => {range: [0, 255]},
  327. 'kelvin' => {range: [0, 100], update_field: 'color_temp', update_min: 153, update_max: 370},
  328. 'color_temp' => {range: [153, 370]},
  329. 'hue' => {range: [0, 359]},
  330. 'saturation' => {range: [0, 100]}
  331. }.each do |field, params|
  332. min, max = params[:range]
  333. update_min = params[:update_min] || min
  334. update_max = params[:update_max] || max
  335. update_field = params[:update_field] || field
  336. it "should support field '#{field}' min --> max" do
  337. seen_updates = []
  338. @client.patch_state({'status' => 'ON', field => min}, @id_params)
  339. @mqtt_client.on_update(@id_params) do |id, message|
  340. seen_updates << message
  341. message[update_field] == update_max
  342. end
  343. @client.patch_state({field => max, 'transition' => 1.0}, @id_params)
  344. @mqtt_client.wait_for_listeners
  345. transitions_are_equal(
  346. expected: calculate_transition_steps(start_value: update_min, end_value: update_max, duration: 1000),
  347. seen: seen_updates.map{ |x| x[update_field] },
  348. allowed_variation: 3
  349. )
  350. end
  351. it "should support field '#{field}' max --> min" do
  352. seen_updates = []
  353. @client.patch_state({'status' => 'ON', field => max}, @id_params)
  354. @mqtt_client.on_update(@id_params) do |id, message|
  355. seen_updates << message
  356. message[update_field] == update_min
  357. end
  358. @client.patch_state({field => min, 'transition' => 1.0}, @id_params)
  359. @mqtt_client.wait_for_listeners
  360. transitions_are_equal(
  361. expected: calculate_transition_steps(start_value: update_max, end_value: update_min, duration: 1000),
  362. seen: seen_updates.map{ |x| x[update_field] },
  363. allowed_variation: 3
  364. )
  365. end
  366. end
  367. end
  368. context 'color support' do
  369. it 'should support color transitions' do
  370. response = @client.schedule_transition(@id_params, {
  371. field: 'color',
  372. start_value: '255,0,0',
  373. end_value: '0,255,0',
  374. duration: 1.0,
  375. period: 500
  376. })
  377. expect(response['success']).to eq(true)
  378. end
  379. it 'should smoothly transition from one color to another' do
  380. seen_updates = []
  381. end_color = '0,255,0'
  382. end_hs = rgb_to_hs(end_color)
  383. last_hue = nil
  384. last_sat = nil
  385. @mqtt_client.on_update(@id_params) do |id, message|
  386. field, value = message.first
  387. if field == 'hue'
  388. last_hue = value
  389. elsif field == 'saturation'
  390. last_sat = value
  391. end
  392. if !last_hue.nil? && !last_sat.nil?
  393. seen_updates << {hue: last_hue, saturation: last_sat}
  394. end
  395. last_hue == end_hs[:hue] && last_sat == end_hs[:saturation]
  396. end
  397. response = @client.schedule_transition(@id_params, {
  398. field: 'color',
  399. start_value: '255,0,0',
  400. end_value: '0,255,0',
  401. duration: 4.0,
  402. period: 1000
  403. })
  404. @mqtt_client.wait_for_listeners
  405. # This ends up being less even than you'd expect because RGB -> Hue/Sat is lossy.
  406. # Raw logs show that the right thing is happening:
  407. #
  408. # >>> stepSizes = (-64,64,0)
  409. # >>> start = (255,0,0)
  410. # >>> end = (0,255,0)
  411. # >>> current color = (191,64,0)
  412. # >>> current color = (127,128,0)
  413. # >>> current color = (63,192,0)
  414. # >>> current color = (0,255,0)
  415. expected_updates = calculate_color_transition_steps(start_color: '255,0,0', end_color: '0,255,0', duration: 4000, period: 1000)
  416. color_transitions_are_equal(
  417. expected: expected_updates,
  418. seen: seen_updates
  419. )
  420. end
  421. it 'should handle color transitions from known state' do
  422. seen_updates = []
  423. @client.patch_state({status: 'ON', color: '255,0,0'}, @id_params)
  424. end_color = '0,0,255'
  425. end_hs = rgb_to_hs(end_color)
  426. last_hue = nil
  427. last_sat = nil
  428. @mqtt_client.on_update(@id_params) do |id, message|
  429. field, value = message.first
  430. if field == 'hue'
  431. last_hue = value
  432. elsif field == 'saturation'
  433. last_sat = value
  434. end
  435. if !last_hue.nil? && !last_sat.nil?
  436. seen_updates << {hue: last_hue, saturation: last_sat}
  437. end
  438. last_hue == end_hs[:hue] && last_sat == end_hs[:saturation]
  439. end
  440. @client.patch_state({color: '0,0,255', transition: 2.0}, @id_params)
  441. @mqtt_client.wait_for_listeners
  442. expected_updates = calculate_color_transition_steps(start_color: '255,0,0', end_color: '0,0,255', duration: 2000)
  443. color_transitions_are_equal(
  444. expected: expected_updates,
  445. seen: seen_updates
  446. )
  447. end
  448. end
  449. context 'computed parameters' do
  450. (@transition_defaults = {
  451. duration: {default: 4.5, test: 2},
  452. num_periods: {default: 10, test: 5},
  453. period: {default: 450, test: 225}
  454. }).each do |k, params|
  455. it "it should compute other parameters given only #{k}" do
  456. seen_values = 0
  457. gap = 0
  458. @mqtt_client.on_update(@id_params) do |id, msg|
  459. val = msg['brightness']
  460. if val > 0
  461. seen_values += 1
  462. last_seen = val
  463. end
  464. if seen_values == 3
  465. gap = last_seen/seen_values
  466. end
  467. val == 255
  468. end
  469. t_params = {field: 'level', start_value: 0, end_value: 100}.merge({k => params[:test]})
  470. start_time = Time.now
  471. @client.schedule_transition(@id_params, t_params)
  472. transitions = @client.transitions
  473. @mqtt_client.wait_for_listeners
  474. duration = Time.now - start_time
  475. expect(transitions.length).to eq(1), "Should only be one active transition"
  476. period = transitions.first['period']
  477. expected_duration = (k == :duration ? params[:test] : (TransitionHelpers::Defaults::DURATION/1000.0))
  478. num_periods = (expected_duration/period.to_f)*1000
  479. expect(duration).to be_within(1.5).of(expected_duration)
  480. expect(gap).to be_within(10).of((255/num_periods).ceil)
  481. end
  482. end
  483. end
  484. end