transition_spec.rb 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. it 'should support creating long transitions' do
  136. @client.patch_state({status: 'ON', level: 0}, @id_params)
  137. @client.patch_state({level: 100, transition: 60000}, @id_params)
  138. t = @client.transitions.last
  139. calculated_duration = t['period'] * (100.to_f / t['step_size'])
  140. expect(calculated_duration).to be_within(100).of(60000*1000), "Calculated duration should be close to 600s"
  141. end
  142. end
  143. context 'transition packets' do
  144. it 'should send an initial state packet' do
  145. seen = false
  146. @mqtt_client.on_update(@id_params) do |id, message|
  147. seen = message['brightness'] == 0
  148. end
  149. @client.schedule_transition(@id_params, @transition_params)
  150. @mqtt_client.wait_for_listeners
  151. expect(seen).to be(true)
  152. end
  153. it 'should respect the period parameter' do
  154. seen_updates = []
  155. start_time = Time.now
  156. @mqtt_client.on_update(@id_params) do |id, message|
  157. seen_updates << message
  158. message['brightness'] == 255
  159. end
  160. @client.schedule_transition(@id_params, @transition_params.merge(duration: 2.0, period: 500))
  161. @mqtt_client.wait_for_listeners
  162. expected_updates = calculate_transition_steps(start_value: 0, end_value: 255, period: 500, duration: 2000)
  163. transitions_are_equal(
  164. expected: expected_updates,
  165. seen: seen_updates.map { |x| x['brightness'] },
  166. # Allow some variation for the lossy level -> brightness conversion
  167. allowed_variation: 2
  168. )
  169. expect((Time.now - start_time)/4).to be >= 0.5 # Don't count the first update
  170. end
  171. it 'should support two transitions for different devices at the same time' do
  172. id1 = @id_params
  173. id2 = @id_params.merge(type: 'fut089')
  174. @client.schedule_transition(id1, @transition_params)
  175. @client.schedule_transition(id2, @transition_params)
  176. id1_updates = []
  177. id2_updates = []
  178. @mqtt_client.on_update do |id, msg|
  179. if id[:type] == id1[:type]
  180. id1_updates << msg
  181. else
  182. id2_updates << msg
  183. end
  184. id1_updates.length == @num_transition_updates && id2_updates.length == @num_transition_updates
  185. end
  186. @mqtt_client.wait_for_listeners
  187. expect(id1_updates.length).to eq(@num_transition_updates)
  188. expect(id2_updates.length).to eq(@num_transition_updates)
  189. end
  190. it 'should assume initial state if one is not provided' do
  191. @client.patch_state({status: 'ON', level: 0}, @id_params)
  192. seen_updates = []
  193. @mqtt_client.on_update(@id_params) do |id, message|
  194. seen_updates << message
  195. message['brightness'] == 255
  196. end
  197. @client.schedule_transition(@id_params, @transition_params.reject { |x| x == :start_value }.merge(duration: 2, period: 500))
  198. @mqtt_client.wait_for_listeners
  199. transitions_are_equal(
  200. expected: calculate_transition_steps(start_value: 0, end_value: 255, duration: 2000, period: 500),
  201. seen: seen_updates.map { |x| x['brightness'] },
  202. # Allow some variation for the lossy level -> brightness conversion
  203. allowed_variation: 2
  204. )
  205. end
  206. end
  207. context 'status transition' do
  208. it 'should transition from off -> on' do
  209. seen_updates = {}
  210. @client.patch_state({status: 'OFF'}, @id_params)
  211. @mqtt_client.on_update(@id_params) do |id, message|
  212. message.each do |k, v|
  213. seen_updates[k] ||= []
  214. seen_updates[k] << v
  215. end
  216. seen_updates['brightness'] && seen_updates['brightness'].last == 255
  217. end
  218. @client.patch_state({status: 'ON', transition: 1.0}, @id_params)
  219. @mqtt_client.wait_for_listeners
  220. expect(seen_updates['state']).to eq(['ON'])
  221. transitions_are_equal(
  222. expected: calculate_transition_steps(start_value: 0, end_value: 255, duration: 1000),
  223. seen: seen_updates['brightness'],
  224. # Allow some variation for the lossy level -> brightness conversion
  225. allowed_variation: 3
  226. )
  227. end
  228. it 'should transition from on -> off' do
  229. seen_updates = {}
  230. @client.patch_state({status: 'ON', level: 100}, @id_params)
  231. @mqtt_client.on_update(@id_params) do |id, message|
  232. message.each do |k, v|
  233. seen_updates[k] ||= []
  234. seen_updates[k] << v
  235. end
  236. seen_updates['state'] == ['OFF']
  237. end
  238. @client.patch_state({status: 'OFF', transition: 1.0}, @id_params)
  239. @mqtt_client.wait_for_listeners
  240. expect(seen_updates['state']).to eq(['OFF'])
  241. transitions_are_equal(
  242. expected: calculate_transition_steps(start_value: 255, end_value: 0, duration: 1000),
  243. seen: seen_updates['brightness'],
  244. # Allow some variation for the lossy level -> brightness conversion
  245. allowed_variation: 3
  246. )
  247. end
  248. it 'should transition from off -> on from 0 to a provided brightness, even when there is a last known brightness' do
  249. seen_updates = {}
  250. @client.patch_state({status: 'ON', brightness: 99}, @id_params)
  251. @client.patch_state({status: 'OFF'}, @id_params)
  252. @mqtt_client.on_update(@id_params) do |id, message|
  253. message.each do |k, v|
  254. seen_updates[k] ||= []
  255. seen_updates[k] << v
  256. end
  257. seen_updates['brightness'] && seen_updates['brightness'].last == 128
  258. end
  259. @client.patch_state({status: 'ON', brightness: 128, transition: 1.0}, @id_params)
  260. @mqtt_client.wait_for_listeners
  261. transitions_are_equal(
  262. expected: calculate_transition_steps(start_value: 0, end_value: 128, duration: 1000),
  263. seen: seen_updates['brightness'],
  264. # Allow some variation for the lossy level -> brightness conversion
  265. allowed_variation: 4
  266. )
  267. end
  268. it 'should transition from off -> on from 0 to 100, even when there is a last known brightness if the bulb is off' do
  269. seen_updates = {}
  270. @client.patch_state({status: 'ON', brightness: 99}, @id_params)
  271. @client.patch_state({status: 'OFF'}, @id_params)
  272. @mqtt_client.on_update(@id_params) do |id, message|
  273. message.each do |k, v|
  274. seen_updates[k] ||= []
  275. seen_updates[k] << v
  276. end
  277. seen_updates['brightness'] && seen_updates['brightness'].last == 255
  278. end
  279. @mqtt_client.patch_state(@id_params, {state: 'ON', transition: 1.0})
  280. @mqtt_client.wait_for_listeners
  281. transitions_are_equal(
  282. expected: calculate_transition_steps(start_value: 0, end_value: 255, duration: 1000),
  283. seen: seen_updates['brightness'],
  284. # Allow some variation for the lossy level -> brightness conversion
  285. allowed_variation: 4
  286. )
  287. end
  288. it 'should transition from last known brightness if the bulb is already on' do
  289. seen_updates = {}
  290. @client.patch_state({status: 'ON', brightness: 99}, @id_params)
  291. @mqtt_client.on_update(@id_params) do |id, message|
  292. message.each do |k, v|
  293. seen_updates[k] ||= []
  294. seen_updates[k] << v
  295. end
  296. seen_updates['brightness'] && seen_updates['brightness'].last == 255
  297. end
  298. @mqtt_client.patch_state(@id_params, {state: 'ON', brightness: 255, transition: 1.0})
  299. @mqtt_client.wait_for_listeners
  300. transitions_are_equal(
  301. expected: calculate_transition_steps(start_value: 99, end_value: 255, duration: 1000),
  302. seen: seen_updates['brightness'],
  303. # Allow some variation for the lossy level -> brightness conversion
  304. allowed_variation: 4
  305. )
  306. end
  307. it 'should transition from on -> off with known last brightness' do
  308. seen_updates = {}
  309. @client.patch_state({status: 'ON', brightness: 99}, @id_params)
  310. @mqtt_client.on_update(@id_params) do |id, message|
  311. message.each do |k, v|
  312. seen_updates[k] ||= []
  313. seen_updates[k] << v
  314. end
  315. seen_updates['state'] == ['OFF']
  316. end
  317. @client.patch_state({status: 'OFF', transition: 1.0}, @id_params)
  318. @mqtt_client.wait_for_listeners
  319. transitions_are_equal(
  320. expected: calculate_transition_steps(start_value: 99, end_value: 0, duration: 1000),
  321. seen: seen_updates['brightness'],
  322. # Allow some variation for the lossy level -> brightness conversion
  323. allowed_variation: 3
  324. )
  325. end
  326. it 'should not transition to 100% if a brightness is specified' do
  327. seen_updates = {}
  328. # Set a last known level
  329. @client.patch_state({status: 'ON', level: 0}, @id_params)
  330. @client.patch_state({status: 'OFF'}, @id_params)
  331. @mqtt_client.on_update(@id_params) do |id, message|
  332. message.each do |k, v|
  333. seen_updates[k] ||= []
  334. seen_updates[k] << v
  335. end
  336. seen_updates['brightness'] && seen_updates['brightness'].last == 128
  337. end
  338. @client.patch_state({status: 'ON', brightness: 128, transition: 1.0}, @id_params)
  339. @mqtt_client.wait_for_listeners
  340. expect(seen_updates['state']).to eq(['ON'])
  341. transitions_are_equal(
  342. expected: calculate_transition_steps(start_value: 0, end_value: 128, duration: 1000),
  343. seen: seen_updates['brightness'],
  344. # Allow some variation for the lossy level -> brightness conversion
  345. allowed_variation: 3
  346. )
  347. end
  348. end
  349. context 'field support' do
  350. {
  351. 'level' => {range: [0, 100], update_field: 'brightness', update_max: 255},
  352. 'brightness' => {range: [0, 255]},
  353. 'kelvin' => {range: [0, 100], update_field: 'color_temp', update_min: 153, update_max: 370},
  354. 'color_temp' => {range: [153, 370]},
  355. 'hue' => {range: [0, 359]},
  356. 'saturation' => {range: [0, 100]}
  357. }.each do |field, params|
  358. min, max = params[:range]
  359. update_min = params[:update_min] || min
  360. update_max = params[:update_max] || max
  361. update_field = params[:update_field] || field
  362. it "should support field '#{field}' min --> max" do
  363. seen_updates = []
  364. @client.patch_state({'status' => 'ON', field => min}, @id_params)
  365. @mqtt_client.on_update(@id_params) do |id, message|
  366. seen_updates << message
  367. message[update_field] == update_max
  368. end
  369. @client.patch_state({field => max, 'transition' => 1.0}, @id_params)
  370. @mqtt_client.wait_for_listeners
  371. transitions_are_equal(
  372. expected: calculate_transition_steps(start_value: update_min, end_value: update_max, duration: 1000),
  373. seen: seen_updates.map{ |x| x[update_field] },
  374. allowed_variation: 3
  375. )
  376. end
  377. it "should support field '#{field}' max --> min" do
  378. seen_updates = []
  379. @client.patch_state({'status' => 'ON', field => max}, @id_params)
  380. @mqtt_client.on_update(@id_params) do |id, message|
  381. seen_updates << message
  382. message[update_field] == update_min
  383. end
  384. @client.patch_state({field => min, 'transition' => 1.0}, @id_params)
  385. @mqtt_client.wait_for_listeners
  386. transitions_are_equal(
  387. expected: calculate_transition_steps(start_value: update_max, end_value: update_min, duration: 1000),
  388. seen: seen_updates.map{ |x| x[update_field] },
  389. allowed_variation: 3
  390. )
  391. end
  392. end
  393. end
  394. context 'color support' do
  395. it 'should support color transitions' do
  396. response = @client.schedule_transition(@id_params, {
  397. field: 'color',
  398. start_value: '255,0,0',
  399. end_value: '0,255,0',
  400. duration: 1.0,
  401. period: 500
  402. })
  403. expect(response['success']).to eq(true)
  404. end
  405. it 'should smoothly transition from one color to another' do
  406. seen_updates = []
  407. end_color = '0,255,0'
  408. end_hs = rgb_to_hs(end_color)
  409. last_hue = nil
  410. last_sat = nil
  411. @mqtt_client.on_update(@id_params) do |id, message|
  412. field, value = message.first
  413. if field == 'hue'
  414. last_hue = value
  415. elsif field == 'saturation'
  416. last_sat = value
  417. end
  418. if !last_hue.nil? && !last_sat.nil?
  419. seen_updates << {hue: last_hue, saturation: last_sat}
  420. end
  421. last_hue == end_hs[:hue] && last_sat == end_hs[:saturation]
  422. end
  423. response = @client.schedule_transition(@id_params, {
  424. field: 'color',
  425. start_value: '255,0,0',
  426. end_value: '0,255,0',
  427. duration: 4.0,
  428. period: 1000
  429. })
  430. @mqtt_client.wait_for_listeners
  431. # This ends up being less even than you'd expect because RGB -> Hue/Sat is lossy.
  432. # Raw logs show that the right thing is happening:
  433. #
  434. # >>> stepSizes = (-64,64,0)
  435. # >>> start = (255,0,0)
  436. # >>> end = (0,255,0)
  437. # >>> current color = (191,64,0)
  438. # >>> current color = (127,128,0)
  439. # >>> current color = (63,192,0)
  440. # >>> current color = (0,255,0)
  441. expected_updates = calculate_color_transition_steps(start_color: '255,0,0', end_color: '0,255,0', duration: 4000, period: 1000)
  442. color_transitions_are_equal(
  443. expected: expected_updates,
  444. seen: seen_updates
  445. )
  446. end
  447. it 'should handle color transitions from known state' do
  448. seen_updates = []
  449. @client.patch_state({status: 'ON', color: '255,0,0'}, @id_params)
  450. end_color = '0,0,255'
  451. end_hs = rgb_to_hs(end_color)
  452. last_hue = nil
  453. last_sat = nil
  454. @mqtt_client.on_update(@id_params) do |id, message|
  455. field, value = message.first
  456. if field == 'hue'
  457. last_hue = value
  458. elsif field == 'saturation'
  459. last_sat = value
  460. end
  461. if !last_hue.nil? && !last_sat.nil?
  462. seen_updates << {hue: last_hue, saturation: last_sat}
  463. end
  464. last_hue == end_hs[:hue] && last_sat == end_hs[:saturation]
  465. end
  466. @client.patch_state({color: '0,0,255', transition: 2.0}, @id_params)
  467. @mqtt_client.wait_for_listeners
  468. expected_updates = calculate_color_transition_steps(start_color: '255,0,0', end_color: '0,0,255', duration: 2000)
  469. color_transitions_are_equal(
  470. expected: expected_updates,
  471. seen: seen_updates
  472. )
  473. end
  474. end
  475. context 'computed parameters' do
  476. (@transition_defaults = {
  477. duration: {default: 4.5, test: 2},
  478. num_periods: {default: 10, test: 5},
  479. period: {default: 450, test: 225}
  480. }).each do |k, params|
  481. it "it should compute other parameters given only #{k}" do
  482. seen_values = 0
  483. gap = 0
  484. @mqtt_client.on_update(@id_params) do |id, msg|
  485. val = msg['brightness']
  486. if val > 0
  487. seen_values += 1
  488. last_seen = val
  489. end
  490. if seen_values == 3
  491. gap = last_seen/seen_values
  492. end
  493. val == 255
  494. end
  495. t_params = {field: 'level', start_value: 0, end_value: 100}.merge({k => params[:test]})
  496. start_time = Time.now
  497. @client.schedule_transition(@id_params, t_params)
  498. transitions = @client.transitions
  499. @mqtt_client.wait_for_listeners
  500. duration = Time.now - start_time
  501. expect(transitions.length).to eq(1), "Should only be one active transition"
  502. period = transitions.first['period']
  503. expected_duration = (k == :duration ? params[:test] : (TransitionHelpers::Defaults::DURATION/1000.0))
  504. num_periods = (expected_duration/period.to_f)*1000
  505. expect(duration).to be_within(1.5).of(expected_duration)
  506. expect(gap).to be_within(10).of((255/num_periods).ceil)
  507. end
  508. end
  509. end
  510. end