transition_spec.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 with known last 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 == 255
  251. end
  252. @client.patch_state({status: 'ON', transition: 1.0}, @id_params)
  253. @mqtt_client.wait_for_listeners
  254. transitions_are_equal(
  255. expected: calculate_transition_steps(start_value: 99, end_value: 255, 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 on -> off with known last brightness' do
  262. seen_updates = {}
  263. @client.patch_state({status: 'ON', brightness: 99}, @id_params)
  264. @mqtt_client.on_update(@id_params) do |id, message|
  265. message.each do |k, v|
  266. seen_updates[k] ||= []
  267. seen_updates[k] << v
  268. end
  269. seen_updates['state'] == ['OFF']
  270. end
  271. @client.patch_state({status: 'OFF', transition: 1.0}, @id_params)
  272. @mqtt_client.wait_for_listeners
  273. transitions_are_equal(
  274. expected: calculate_transition_steps(start_value: 99, end_value: 0, duration: 1000),
  275. seen: seen_updates['brightness'],
  276. # Allow some variation for the lossy level -> brightness conversion
  277. allowed_variation: 3
  278. )
  279. end
  280. end
  281. context 'field support' do
  282. {
  283. 'level' => {range: [0, 100], update_field: 'brightness', update_max: 255},
  284. 'brightness' => {range: [0, 255]},
  285. 'kelvin' => {range: [0, 100], update_field: 'color_temp', update_min: 153, update_max: 370},
  286. 'color_temp' => {range: [153, 370]},
  287. 'hue' => {range: [0, 359]},
  288. 'saturation' => {range: [0, 100]}
  289. }.each do |field, params|
  290. min, max = params[:range]
  291. update_min = params[:update_min] || min
  292. update_max = params[:update_max] || max
  293. update_field = params[:update_field] || field
  294. it "should support field '#{field}' min --> max" do
  295. seen_updates = []
  296. @client.patch_state({'status' => 'ON', field => min}, @id_params)
  297. @mqtt_client.on_update(@id_params) do |id, message|
  298. seen_updates << message
  299. message[update_field] == update_max
  300. end
  301. @client.patch_state({field => max, 'transition' => 1.0}, @id_params)
  302. @mqtt_client.wait_for_listeners
  303. transitions_are_equal(
  304. expected: calculate_transition_steps(start_value: update_min, end_value: update_max, duration: 1000),
  305. seen: seen_updates.map{ |x| x[update_field] },
  306. allowed_variation: 3
  307. )
  308. end
  309. it "should support field '#{field}' max --> min" do
  310. seen_updates = []
  311. @client.patch_state({'status' => 'ON', field => max}, @id_params)
  312. @mqtt_client.on_update(@id_params) do |id, message|
  313. seen_updates << message
  314. message[update_field] == update_min
  315. end
  316. @client.patch_state({field => min, 'transition' => 1.0}, @id_params)
  317. @mqtt_client.wait_for_listeners
  318. transitions_are_equal(
  319. expected: calculate_transition_steps(start_value: update_max, end_value: update_min, duration: 1000),
  320. seen: seen_updates.map{ |x| x[update_field] },
  321. allowed_variation: 3
  322. )
  323. end
  324. end
  325. end
  326. context 'color support' do
  327. it 'should support color transitions' do
  328. response = @client.schedule_transition(@id_params, {
  329. field: 'color',
  330. start_value: '255,0,0',
  331. end_value: '0,255,0',
  332. duration: 1.0,
  333. period: 500
  334. })
  335. expect(response['success']).to eq(true)
  336. end
  337. it 'should smoothly transition from one color to another' do
  338. seen_updates = []
  339. end_color = '0,255,0'
  340. end_hs = rgb_to_hs(end_color)
  341. last_hue = nil
  342. last_sat = nil
  343. @mqtt_client.on_update(@id_params) do |id, message|
  344. field, value = message.first
  345. if field == 'hue'
  346. last_hue = value
  347. elsif field == 'saturation'
  348. last_sat = value
  349. end
  350. if !last_hue.nil? && !last_sat.nil?
  351. seen_updates << {hue: last_hue, saturation: last_sat}
  352. end
  353. last_hue == end_hs[:hue] && last_sat == end_hs[:saturation]
  354. end
  355. response = @client.schedule_transition(@id_params, {
  356. field: 'color',
  357. start_value: '255,0,0',
  358. end_value: '0,255,0',
  359. duration: 4.0,
  360. period: 1000
  361. })
  362. @mqtt_client.wait_for_listeners
  363. # This ends up being less even than you'd expect because RGB -> Hue/Sat is lossy.
  364. # Raw logs show that the right thing is happening:
  365. #
  366. # >>> stepSizes = (-64,64,0)
  367. # >>> start = (255,0,0)
  368. # >>> end = (0,255,0)
  369. # >>> current color = (191,64,0)
  370. # >>> current color = (127,128,0)
  371. # >>> current color = (63,192,0)
  372. # >>> current color = (0,255,0)
  373. expected_updates = calculate_color_transition_steps(start_color: '255,0,0', end_color: '0,255,0', duration: 4000, period: 1000)
  374. color_transitions_are_equal(
  375. expected: expected_updates,
  376. seen: seen_updates
  377. )
  378. end
  379. it 'should handle color transitions from known state' do
  380. seen_updates = []
  381. @client.patch_state({status: 'ON', color: '255,0,0'}, @id_params)
  382. end_color = '0,0,255'
  383. end_hs = rgb_to_hs(end_color)
  384. last_hue = nil
  385. last_sat = nil
  386. @mqtt_client.on_update(@id_params) do |id, message|
  387. field, value = message.first
  388. if field == 'hue'
  389. last_hue = value
  390. elsif field == 'saturation'
  391. last_sat = value
  392. end
  393. if !last_hue.nil? && !last_sat.nil?
  394. seen_updates << {hue: last_hue, saturation: last_sat}
  395. end
  396. last_hue == end_hs[:hue] && last_sat == end_hs[:saturation]
  397. end
  398. @client.patch_state({color: '0,0,255', transition: 2.0}, @id_params)
  399. @mqtt_client.wait_for_listeners
  400. expected_updates = calculate_color_transition_steps(start_color: '255,0,0', end_color: '0,0,255', duration: 2000)
  401. color_transitions_are_equal(
  402. expected: expected_updates,
  403. seen: seen_updates
  404. )
  405. end
  406. end
  407. context 'computed parameters' do
  408. # it 'should accept no length parameters' do
  409. # result = @client.schedule_transition(@id_params, field: 'kelvin', end_value: 100)
  410. # puts @transition_defaults
  411. # expect(result).to eq({'success' => true})
  412. # end
  413. (@transition_defaults = {
  414. duration: {default: 4.5, test: 2},
  415. num_periods: {default: 10, test: 5},
  416. period: {default: 450, test: 225}
  417. }).each do |k, params|
  418. it "it should compute other parameters given only #{k}" do
  419. seen_values = 0
  420. gap = 0
  421. @mqtt_client.on_update(@id_params) do |id, msg|
  422. val = msg['brightness']
  423. if val > 0
  424. seen_values += 1
  425. last_seen = val
  426. end
  427. if seen_values == 3
  428. gap = last_seen/seen_values
  429. end
  430. val == 255
  431. end
  432. t_params = {field: 'level', start_value: 0, end_value: 100}.merge({k => params[:test]})
  433. start_time = Time.now
  434. @client.schedule_transition(@id_params, t_params)
  435. transitions = @client.transitions
  436. @mqtt_client.wait_for_listeners
  437. duration = Time.now - start_time
  438. expect(transitions.length).to eq(1), "Should only be one active transition"
  439. period = transitions.first['period']
  440. expected_duration = (k == :duration ? params[:test] : (TransitionHelpers::Defaults::DURATION/1000.0))
  441. num_periods = (expected_duration/period.to_f)*1000
  442. expect(duration).to be_within(1.5).of(expected_duration)
  443. expect(gap).to be_within(10).of((255/num_periods).ceil)
  444. end
  445. end
  446. end
  447. end